#!/usr/bin/env node

/**
 * NodeOS
 *
 * @copyright 2013-2017 Jacob Groundwater, Jesús Leganés-Combarro 'piranna'
 *  and other contributors
 *
 * @license MIT
 */

const readdir = require('fs').readdir

const upload = require('prebuild/upload')


const PREBUILDS = 'prebuilds'

const PKG = require('../package.json')

const token = process.env.GITHUB_TOKEN
if(!token) throw 'GITHUB_TOKEN environment variable is not defined'

var prerelease = (new Date()).toISOString().substr(0,10)

const branch_name = process.env.BRANCH_NAME
if(branch_name && branch_name !== 'master') return

readdir(PREBUILDS, function(err, files)
{
  if(err) throw err

  const opts =
  {
    upload: token,
    pkg: PKG,
    prerelease: prerelease,
    files: files.map(function(file){return PREBUILDS+'/'+file})
  }

  upload(opts, function(error)
  {
    if(error) throw error
  })
})
